﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sql;
using System.Data.SqlClient;
using System.Data;
using DevExpress.Web.ASPxGridView;

public partial class KullaniciTanimlama : System.Web.UI.Page
{

    private DcSql conn, connMikroDB;
    private string VT_Prim = "Prim";
    
    protected void Page_Load(object sender, EventArgs e)
    {
        conn = new DcSql();
        string HataMesaji = conn.DBBaglan(VT_Prim);
        Yenile();
    }


    private void Yenile()
    {
        string selectsql = "SELECT * FROM Kullanıcı ";

        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(selectsql, conn.GetConn());

        da.Fill(ds, "Kullanıcı");

        ASPxGridView1.DataSource = ds.Tables[0];

        if (!IsPostBack && !IsCallback)
            ASPxGridView1.DataBind();
    }

    protected void ASPxGridView1_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
        SqlCommand cmd = new SqlCommand("KullaniciSil", conn.GetConn());
        cmd.CommandType = CommandType.StoredProcedure;

        ASPxGridView gridView = (ASPxGridView)sender;
        int index = gridView.FindVisibleIndexByKeyValue(e.Keys[gridView.KeyFieldName]);

        string kullanicID = gridView.GetRowValues(index, gridView.KeyFieldName).ToString();
        cmd.Parameters.AddWithValue("@KulaniciId", kullanicID);
        cmd.ExecuteScalar();

        e.Cancel = true;
        gridView.CancelEdit();
        Yenile();
    }

    protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        SqlCommand cmd = new SqlCommand("KullaniciEkle", conn.GetConn());
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@sKullaniciAdi", e.NewValues["sKullaniciAdi"]);
        cmd.Parameters.AddWithValue("@sKullaniciKod", e.NewValues["sKullaniciKod"]);
        cmd.Parameters.AddWithValue("@sSifre", e.NewValues["sSifre"]);
        cmd.Parameters.AddWithValue("@nYetkiTuru", e.NewValues["nYetkiTuru"]);
       
        cmd.ExecuteScalar();

        e.Cancel = true;
        ASPxGridView1.CancelEdit();
        Yenile();
    }

    protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        SqlCommand cmd = new SqlCommand("KullaniciDuzelt", conn.GetConn());
        cmd.CommandType = CommandType.StoredProcedure;

        ASPxGridView gridView = (ASPxGridView)sender;
        e.NewValues["KullaniciID"] = gridView.GetRowValues(gridView.EditingRowVisibleIndex, "KullaniciID").ToString();

        cmd.Parameters.AddWithValue("@KullaniciID", e.NewValues["KullaniciID"]);
        cmd.Parameters.AddWithValue("@sKullaniciAdi", e.NewValues["sKullaniciAdi"]);
        cmd.Parameters.AddWithValue("@sKullaniciKod", e.NewValues["sKullaniciKod"]);
        cmd.Parameters.AddWithValue("@sSifre", e.NewValues["sSifre"]);
        cmd.Parameters.AddWithValue("@nYetkiTuru", e.NewValues["nYetkiTuru"]);

        cmd.ExecuteScalar();

        e.Cancel = true;
        ASPxGridView1.CancelEdit();
        Yenile();
    }
}